﻿using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Linq;

namespace Spider.YinFans
{
    class Program
    {
        static async Task Main(string[] args)
        {
            if (!Directory.Exists("success"))
            {
                Directory.CreateDirectory("success");
            }
            if (!Directory.Exists("yinfans"))
            {
                Directory.CreateDirectory("yinfans");
            }

            while (true)
            {
                var httpClient = new HttpClient();
                httpClient.BaseAddress = new Uri("https://www.yinfans.net");

                var startIndex = 0;
                if (File.Exists("index.txt"))
                {
                    var inxString = await File.ReadAllTextAsync("index.txt");
                    startIndex = int.Parse(inxString) + 1;
                }

                int count = 0;
                for (var i = startIndex; i < startIndex + 5000; i++)
                {
                    try
                    {
                        Console.WriteLine($"开始处理序号[{i}]");
                        var response = await httpClient.GetAsync($"/movie/{i}");
                        Console.WriteLine($"{i}\t : {response.StatusCode}");
                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();
                            await File.WriteAllTextAsync($"yinfans/{i}.html", content);
                            var prefix = i.ToString("00000");
                            await Analyzing(prefix, content);
                            count++;
                            await File.WriteAllTextAsync("index.txt", i.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"== {i} 发生错误 ==");
                        Console.WriteLine(ex);
                        await File.AppendAllTextAsync("error.txt", "\n");
                        await File.AppendAllTextAsync("error.txt", $"----------{DateTime.Now}-------------");
                        await File.AppendAllTextAsync("error.txt", $"错误序号{i}");
                        await File.AppendAllTextAsync("error.txt", ex.Message);
                    }
                    await Task.Delay(500);
                }

                if (count == 0)
                {
                    break;
                }
            }
            Console.WriteLine("全部处理完成");
        }

        static async Task Analyzing(string prefix, string html)
        {
            var document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);

            var hrefs = document.DocumentNode.SelectNodes("//a");
            if (hrefs.Any(a => a.GetAttributeValue("href", "").StartsWith("magnet:")))
            {
                var title = document.DocumentNode.SelectSingleNode("//title")?.InnerText;
                if (string.IsNullOrWhiteSpace(title))
                {
                    title = prefix;
                }
                Console.WriteLine($"Title: {title}");
                foreach (var invalidChar in Path.GetInvalidFileNameChars())
                {
                    title = title.Replace(invalidChar, '_');
                }
                title = title.Replace('ç', ' ');
                title = title.Replace(' ', '_');
                title = title.Replace('-', '_');
                title.Replace("#", string.Empty);
                title.Replace("+", string.Empty);


                title = title.Replace("音范丝", "");
                title = title.Replace("影音集", "");
                title = title.TrimEnd('_');

                title = title.Replace("__", "_");
                title = title.Replace("__", "_");
                title = title.Replace("__", "_");

                title = title.Substring(0, title.Length > 240 ? 240 : title.Length);

                var content = document.DocumentNode.SelectSingleNode("//*[@id='content']");
                var body = document.DocumentNode.SelectSingleNode("/html/body");
                body.RemoveAllChildren();
                body.AppendChild(content);

                var styles = document.DocumentNode.SelectNodes("/html/head/style");
                var head = document.DocumentNode.SelectSingleNode("/html/head");
                head.RemoveAllChildren();

                var link1 = document.CreateElement("link");
                link1.SetAttributeValue("type", "text/css");
                link1.SetAttributeValue("rel", "stylesheet");
                link1.SetAttributeValue("href", "https://www.yinfans.net/wp-content/themes/Loos2/css/style/black.css");
                head.AppendChild(link1);

                var link2 = document.CreateElement("link");
                link2.SetAttributeValue("type", "text/css");
                link2.SetAttributeValue("rel", "stylesheet");
                link2.SetAttributeValue("href", "https://s33.czmhgz.cn/wp-content/themes/Loos2/css/style/down_url.css?ver=87666?ref=");
                head.AppendChild(link2);

                head.AppendChildren(styles);

                var comments = document.DocumentNode.SelectNodes("//comment()");
                if (comments != null && comments.Count > 0)
                {
                    foreach (var comment in comments)
                    {
                        comment.Remove();
                    }
                }

                var infos = document.DocumentNode.SelectNodes("//*[@class='article_info']");
                if (infos != null && infos.Count > 0)
                {
                    foreach (var div in infos)
                    {
                        div.RemoveAllChildren();
                    }
                }

                var tags = document.DocumentNode.SelectNodes("//*[@class='article_tags']");
                if (tags != null && tags.Count > 0)
                {
                    foreach (var div in tags)
                    {
                        div.RemoveAllChildren();
                    }
                }

                document.DocumentNode.SelectSingleNode("//*[@id='cililian']")?.Remove();
                document.DocumentNode.SelectSingleNode("//*[@id='showall']")?.Remove();

                var trs = document.DocumentNode.SelectNodes("//*[@id='cili']//tr");
                if (trs != null && trs.Count > 0)
                {
                    foreach (var tr in trs)
                    {
                        tr.SetAttributeValue("style", "display: table-row;");
                    }
                }

                document.Save($"success/{prefix}_{title}.html");
                Console.WriteLine($"处理成功： {title}");
            }
            else
            {
                Console.WriteLine($"无资源");
            }
        }
    }
}
